package com.company.arrays;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MajorityElement {
    public List<Integer> majorityElement(int[] nums) {
        int majorityNum = nums.length / 3;
        List<Integer> result = new ArrayList<>();
        HashMap<Integer, Integer> countMap = new HashMap<>();
        for (int element : nums) {
            countMap.put(element, countMap.get(element) == null ? 1 : countMap.get(element) + 1);
        }

        for (Integer element : countMap.keySet()) {
            if (countMap.get(element) > majorityNum) {
                result.add(element);
            }
        }
        return result;
    }
}
